package com.zxh.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.zxh.dao.ApplicantDao;
import com.zxh.dao.ResumeDao;
import com.zxh.entity.Applicant;
import com.zxh.entity.ResumeBasicInfo;
import com.zxh.utils.CookieEncrypTool;
@WebServlet("/login.do")

public class ApplicantServlet extends HttpServlet{
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		/*req.setCharacterEncoding("utf-8");//设置请求编码格式
		resp.setContentType("text/html;charset=utf-8");//设置响应的编码格式
		resp.setCharacterEncoding("utf-8");*/
		PrintWriter out = resp.getWriter();
		String email = req.getParameter("email");
		String password = req.getParameter("password");
		String rememberMe = req.getParameter("rememberMe");
		//System.out.println(rememberMe);
		ApplicantDao adao = new ApplicantDao();
		//第一次请求的时候，服务端会自动产生一个sessionID返回给客户端，客户端利用cookie技术将其保存在浏览器
		HttpSession session = req.getSession();//session 对象 会话  请求 响应(会话期)
		String sessionId = session.getId();
		//System.out.println(sessionId);
		//boolean flag = adao.login(email, password);//返回登录信息
		Applicant applicant = adao.login(email, password);//返回登录信息
		if(applicant.getApplicant_id()!=0){
			session.setAttribute("username", email);//把用户信息存在session
			session.setAttribute("applicant_id", applicant.getApplicant_id());
			rememberMe(rememberMe,email,password,req,resp);
			ResumeDao resumeDao = new ResumeDao();
			String usertype =applicant.getUsertype();//得到用户类型
			if("0".equals(usertype)){
				//如果登录验证成功，则跳转页面
				ResumeBasicInfo resumBasicInfo=  resumeDao.isExistResme(applicant.getApplicant_id());
				int baseinfo_id = resumBasicInfo.getBasicinfoId();
				session.setAttribute("baseinfo_id", baseinfo_id);
				if(baseinfo_id==0){//简历未填写
					 resp.sendRedirect("applicant/resumeGuide.jsp");
				}else{
					ResumeBasicInfo resume = resumeDao.isExistResme(applicant.getApplicant_id());
					session.setAttribute("basicinfoId", resume.getBasicinfoId());
					resp.sendRedirect("index.jsp");
				}
				}else{
					ResumeBasicInfo resumBasicInfo=  resumeDao.isExistResme(applicant.getApplicant_id());
					int baseinfo_id = resumBasicInfo.getBasicinfoId();
					session.setAttribute("baseinfo_id", baseinfo_id);
					if(baseinfo_id==0){//简历未填写
						 resp.sendRedirect("applicant/resumeGuide.jsp");}
					}
				}else{
				out.print("<script type = 'text/javascript'>");
				out.print("alert('用户名或密码错误，请重新输入');");
				out.print("window.location.href='login.jsp'");
				out.print("</script>");
				//如果登录验证失败，则跳转页面
				/*resp.sendRedirect("login.jsp");*/
			}
		}
	
		
		
	
		
		
		
		
	
	/**
	 * 首先判断“记住密码”复选框是否为选中状态，是：true；否：false.
	 * 
	 * @param rememberMe
	 * @param email
	 * @param password
	 * @param req
	 * @param resp
	 */
	
	private void rememberMe(String rememberMe,String email,String password,HttpServletRequest req, HttpServletResponse resp){
		//如果是true,则需要记住邮箱和密码
		if("true".equals(rememberMe)){
			Cookie cookie = new Cookie("cookie_applicantemail", CookieEncrypTool.encodeBase64(email));
			cookie.setPath("/");//设置cookie的保存路径
			cookie.setMaxAge(7*24*3600);//设置cookie的保存时间
			resp.addCookie(cookie);//将cookie保存到浏览器
			
			cookie = new Cookie("cookie_applicantPassword", CookieEncrypTool.encodeBase64(password));
			cookie.setPath("/");
			cookie.setMaxAge(7*24*3600);//设置cookie的保存时间
			resp.addCookie(cookie);//将cookie保存到浏览器
			
			
		}else{
			//将邮箱和密码清空
			//得到cookie的值
			Cookie[] cookies  = req.getCookies();//获取所有的cookie数据
			if(cookies!=null){
				for(Cookie cookie:cookies){
					if("cookie_applicantemail".equals(cookie.getName())||"cookie_applicantPassword".equals(cookie.getName())){
						cookie.setMaxAge(0);//删除cookie值
						cookie.setPath("/");
						resp.addCookie(cookie);//保存到浏览器
					}
				}
			}
		}
	}

}

